const DB = require('../../utils/db');
const JWT = require('../../utils/jwt');

module.exports = {
	login: async (mobile, password) => {
		// 查看当前人是否存在
		const [list] = await DB.query('select * from t_user where mobile = ? and type=2', [mobile]);
		if (!list.length) throw new Error('当前用户不存在,请先注册！');
		if (list[0].password != password) throw new Error('密码错误！');
		// 生成token,登录成功
		let { id } = list[0];

		DB.query('update t_user set login_time=now() where id=?', [id]);
		let token = JWT.encrypt({ id });
		return token;
	},
	userInfo: async (id) => {
		let [list] = await DB.query(
			`select u.id, u.username, u.mobile, date_format(u.create_time,'%Y-%m-%d %H:%i:%s') createTime,date_format(u.login_time,'%Y-%m-%d %H:%i:%s') loginTime, r.id roleId, r.name roleName, r.routes 
                   from t_user u inner join t_role r on u.role_id = r.id where u.id=? and u.type=2`,
			[id]
		);

		return list[0];
	},
	list: async (pageNum, pageSize, keywords, type) => {
		// 总条数
		let [allList] = await DB.query(`select * from t_user where type=${type}`);

		let aql = `select u.id, u.username,u.email, u.mobile, u.password,u.type, date_format(u.create_time,'%Y-%m-%d %H:%i:%s') createTime,
                        date_format(u.login_time,'%Y-%m-%d %H:%i:%s') loginTime,
                        r.id roleId, r.name roleName from t_user u  left join t_role r on u.role_id=r.id `;

		let arr = [pageSize, (pageNum - 1) * pageSize];

		// 模糊查询
		if (keywords) {
			aql = aql + `where type=${type} and u.username like '%${keywords}%' limit ? offset ? `;
		} else {
			aql = aql + `where type=${type} limit ? offset ? `;
		}

		let [list] = await DB.query(aql, arr);
		return { data: list, total: allList.length };
	},
	add: async (username, mobile, roleId, password) => {
		let [list] = await DB.query('select * from t_user where mobile=? and type=2', [mobile]);
		if (list.length) throw new Error('当前手机号已存在！');
		await DB.query('insert into t_user (username,mobile,role_id,password,type) values(?,?,?,?,2)', [username, mobile, roleId, password]);
	},
	edit: async (username, mobile, roleId, password, id) => {
		if (password) {
			await DB.query('update t_user u set u.username=?, u.mobile=?, u.role_id=?, u.password=? where u.id=?', [username, mobile, roleId, password, id]);
		} else {
			await DB.query('update t_user u set u.username=?, u.mobile=?, u.role_id=? where u.id=?', [username, mobile, roleId, id]);
		}
	},
	detail: async (id) => {
		let [list] = await DB.query('select u.id, u.username, u.mobile, u.password, r.id roleId from t_user u inner join t_role r on u.role_id = r.id where u.id=?&&u.type=2', [id]);
		if (!list.length) throw new Error('用户不存在');
		return list[0];
	},
	del: async (id) => {
		await DB.query('delete from t_user where id = ?', [id]);
	},
	setBloger: async (content) => {
		let [list] = await DB.query('select content from t_bloger where id=1');
		if (list.length) {
			await DB.query('update t_bloger set content=? where id=1', [content]);
		} else {
			await DB.query('insert into t_bloger (content) values(?) where id=1', [content]);
		}
	},
	getBloger: async () => {
		let [list] = await DB.query('select content from t_bloger where id=1');
		return list[0];
	}
};
